package br.edu.ufcg.lp2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;

public class System {
	
	private int nextId = 1;
	private List<Name> names;
	private List<Game> games;
	private User currentUser;
	private Game currentGame;
	private Tag gender;

	public System(User user, Tag gender) throws Exception{
		names = new ArrayList<Name>();
		currentUser = user;
		games = new ArrayList<Game>();
		currentGame = new Game(getNextId());
		this.gender = gender;
	}
	public String getNextId() {
		return String.format("%05d", nextId++);
	}
	public List<Name> getNamesByAlfa() {
		Collections.sort(names, new NameComparator());
		return names;
	}
	public TreeSet<Name> getNamesByRating(){
		return currentGame.getNamesByRating();
	}
	
	public Game getCurrentGame(){
		return currentGame;
	}
	public User getCurrentUser(){
		return currentUser;
	}
	public Tag getGender(){
		return gender;
	}
	
	public List<Name> getRecommedations(){
		return names;
		//to improve this, né
	}
	
	
	public void saveGame(){
		
		FileOutputStream arq = null;
		ObjectOutputStream out = null;
		try {
			arq = new FileOutputStream("teste.txt");
			out = new ObjectOutputStream(arq);
			out.writeObject(currentGame);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				arq.close();
				out.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
	
	
	public Game loadGame(){
		FileInputStream arqRead = null;
		ObjectInputStream in = null;
		Game loadedGame = null;
		try {
			arqRead = new FileInputStream("teste.txt");
			in = new ObjectInputStream(arqRead);
			loadedGame = (Game) in.readObject();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				arqRead.close();
				in.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	 
		return loadedGame;
	}
	
	
	
	public void like(Name name) throws Exception{
		currentGame.like(name, currentUser);
	}
	
	public void dislike(Name name) throws Exception{
		currentGame.dislike(name, currentUser);
	}
	
	public void halflike(Name name) throws Exception{
		currentGame.halflike(name, currentUser);
	}
	
	public void addUseToCurrentGame(User user){
		currentGame.addUser(user);
	}
	public void removeUseToCurrentGame(User user){
		currentGame.removeUser(user);
	}
	
	public void addName(Name name) throws Exception{
		if(names.contains(name)) throw new Exception("This name already exists");
		names.add(name);
	}
	public void removeName(Name name) throws Exception{
		if(!names.contains(name)) throw new Exception("This name does not exist");
		names.remove(name);
	}
	public Name getName(String nameToFind){
		for(Name name: names)
			if (name.getName().equals(nameToFind) && name.hasTag(gender)) return name;
		return null;
	}
	
	public List<Name> find(Tag tag){
		List<Name> found = new ArrayList<Name>();
		for(Name name: names)
			if (name.hasTag(gender))
				for(Tag tagOfName: name.getTags())
						if(tagOfName.equals(tag) )
							found.add(name);
		return found;
		
	} 
	
	public List<Tag> getTags(){
		List<Tag> found = new ArrayList<Tag>();
		for(Name name: names)
			for(Tag tagOfName: name.getTags())
				if(!found.contains(tagOfName))	found.add(tagOfName);
		return found;
	}
	
}
